/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.infrastructure.data.client;

import cn.hallele.infrastructure.data.util.CastUtil;
import cn.hallele.infrastructure.data.util.SpringUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.concurrent.TimeUnit;

/**
 * hallele_mc_core
 *
 * @author anle5
 * @since 2024/9/15 13:22
 */
public class CacheClient {
    private static final RedisTemplate<String, Object> REDIS_TEMPLATE;

    static {
        REDIS_TEMPLATE = SpringUtil.getBean("stringRedisTemplate");
        if (REDIS_TEMPLATE != null) {
            REDIS_TEMPLATE.setKeySerializer(new StringRedisSerializer());
            REDIS_TEMPLATE.setHashKeySerializer(new StringRedisSerializer());
            REDIS_TEMPLATE.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            REDIS_TEMPLATE.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        }
    }

    /**
     * key-value类型的缓存获取
     *
     * @param key key
     * @param <T> 值
     * @return 缓存值
     */
    public static <T> T getValue(String key) {
        return CastUtil.cast(REDIS_TEMPLATE.opsForValue().get(key));
    }

    /**
     * hash类型的缓存获取
     *
     * @param key    key
     * @param subKey 子key
     * @param <T>    值
     * @return 缓存值
     */
    public static <T> T getHashValue(String key, String subKey) {
        return CastUtil.cast(REDIS_TEMPLATE.opsForHash().get(key, subKey));
    }

    /**
     * 校验是否存在hash值
     *
     * @param hashKey hashKey
     * @param key     key
     * @return true：存在  false：不存在
     */
    public static boolean existsHash(String hashKey, String key) {
        return Boolean.TRUE.equals(REDIS_TEMPLATE.opsForHash().hasKey(hashKey, key));
    }

    /**
     * 校验是否存在值
     *
     * @param key key
     * @return true：存在  false：不存在
     */
    public static boolean exists(String key) {
        return Boolean.TRUE.equals(REDIS_TEMPLATE.hasKey(key));
    }

    /**
     * 添加缓存
     *
     * @param key   键
     * @param value 值
     */
    public static void setValue(String key, String value) {
        REDIS_TEMPLATE.opsForValue().set(key, value);
    }

    /**
     * 添加缓存
     *
     * @param key   键
     * @param value 值
     */
    public static void setHashValue(String hashKey, String key, String value) {
        REDIS_TEMPLATE.opsForHash().put(key, hashKey, value);
    }

    /**
     * 添加缓存
     *
     * @param key     键
     * @param value   值
     * @param timeout 过期时间，单位秒
     */
    public static void setValue(String key, String value, long timeout) {
        REDIS_TEMPLATE.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 删除缓存
     *
     * @param key 键
     */
    public static <T> T getAndDelete(String key) {
        return CastUtil.cast(REDIS_TEMPLATE.opsForValue().getAndDelete(key));
    }
}
